package com.obas.view.testplan {
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.event.TestPlanOperationEvent;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.business.TestTaskItem;
   import com.ease.component.managers.ErrorManager;
   import com.ease.component.managers.ProgressManager;

   import flash.events.EventDispatcher;
   import flash.events.MouseEvent;

   import mx.collections.ArrayCollection;
   import mx.events.ListEvent;

   [Event(name="{com.obas.event.TestPlanOperationEvent.REQUEST_ID}", type="com.obas.event.TestPlanOperationEvent")]
   public class TestPlanViewMediator extends EventDispatcher {
      private var _pageId:String;

      public var _view:TestPlanView;

      [InjectableView]
      public function get view():TestPlanView {
         return _view;
      }

      public function set view(value:TestPlanView):void {
         _view = value;
         init();
      }

      private function init():void {
         view.refresh_btn.addEventListener(MouseEvent.CLICK, onRefresh);
         view.run_btn.addEventListener(MouseEvent.CLICK, onRun);
         onRefresh();
      }

      private function onRefresh(e:* = null):void {
         ProgressManager.showProgress(view, ProgressManager.LOADING_SPINNER);
         dispatchEvent(new TestPlanOperationEvent([view.testPlan.id], TestPlanOperationEvent.GET_PLAN_REPORT));
      }

      private function onRun(e:*):void {
         _pageId = "IE" + new Date().time;
         ProgressManager.showProgress(view, ProgressManager.LOADING_SPINNER);
         dispatchEvent(new TestPlanOperationEvent([_pageId, view.testPlan.id], TestPlanOperationEvent.RUN_TEST_PLAN));
      }

      [ResponseHandler("{com.obas.event.TestPlanOperationEvent.RETURN_ID}")]
      public function onTestPlanOperationEvent(event:CommandCompletionEvent):void {
         ProgressManager.hideProgress(view);
         if (event.error) {
            ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
            return ;
         }

         var request:TestPlanOperationEvent = event.request as TestPlanOperationEvent;
         var results:ArrayCollection = event.result as ArrayCollection;
          if (request.actionType == TestPlanOperationEvent.RUN_TEST_PLAN) {
         }
         else if (request.actionType == TestPlanOperationEvent.GET_PLAN_REPORT) {
            var lineDp:ArrayCollection = new ArrayCollection();
            for each (var result:Object in results) {
               var tempConvertSchema:Object = new Object();
               tempConvertSchema["startTime"] = result.task.startTime.getTime();
               var successValue:int = 0;
               var failureValue:int = 0;
               var notEverRun:int = 0;
               for each (var item:TestTaskItem in result.testTaskItems) {
                  if (!item.endTime) {
                     notEverRun++;
                  }
                  else if (item.testResult == true) {
                     successValue++;
                  }
                  else {
                     failureValue++;
                  }
               }
               tempConvertSchema["name"] = result.task.startTime;
               tempConvertSchema["failover"] = failureValue;
               tempConvertSchema["total"] = result.testTaskItems.length;
               tempConvertSchema["failure"] = (failureValue / result.testTaskItems.length * 100).toFixed(1);
               tempConvertSchema["date"] = result.task.startTime;

               var success:Object = new Object();
               success["resultValue"] = successValue;
               success["resultType"] = "成功";
               var failure:Object = new Object();
               failure["resultValue"] = failureValue;
               failure["resultType"] = "失败";
               var notRun:Object = new Object();
               notRun["resultValue"] = notEverRun;
               notRun["resultType"] = "未执行";
               var detail:Array = [success, failure, notRun];
               tempConvertSchema["children"] = [{"detail":detail}];
               lineDp.addItem(tempConvertSchema);
            }
            updateChartStartEnd(lineDp);
            view.lineChart.dataProvider = lineDp;

            view.dpHierarchy = lineDp;
         }
      }

      private function updateChartStartEnd(data:ArrayCollection):void {
         if (data && data.length > 0){
            var max:Number = view.start = data.getItemAt(0)['startTime'] - 100000;
            for (var index:int = 1; index < data.length; index++) {
               if (data.getItemAt(index)['startTime'] > max) {
                  max = data.getItemAt(index)['startTime'];
               }
            }
            view.end = max + 100000;
         }
      }
   }
}